package day07;

import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload2.core.DiskFileItemFactory;
import org.apache.commons.fileupload2.core.FileItem;
import org.apache.commons.fileupload2.core.RequestContext;
import org.apache.commons.fileupload2.jakarta.servlet5.JakartaServletFileUpload;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;

@WebServlet("/upload")
public class FileUploadServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("FileUploadServlet.service()");

        ServletContext application=getServletContext();
        String uploadDir=application.getRealPath("fileupload");

//        String name=request.getParameter("name");
//        String file=request.getParameter("file");
//
//        System.out.println("name = " + name);
//        System.out.println("file = " + file);
//
//        InputStream in=request.getInputStream();
//
//        byte[] b=new byte[20480];
//
//
//        int n;
//
//        while((n=in.read(b))!=-1){
//            System.out.print(new String(b,0,n));
//        }


        //配置上传参数
        DiskFileItemFactory factory=DiskFileItemFactory
                .builder()
                .setBufferSize(20480)
                .get();

        //解析工具
        JakartaServletFileUpload fileUpload=new JakartaServletFileUpload(factory);

        //解析请求
//        List<FileItem> items=fileUpload.parseRequest(request);
//
//
//        for (FileItem item : items) {
//
//            if(item.isFormField()){
//                //非文件数据
//                String fieldName=item.getFieldName();
//                String fieldValue=item.getString();
//
//                System.out.println(fieldName+"="+fieldValue);
//            }else{
//                //文件数据
//                String fieldName=item.getFieldName();
//                String fileName=item.getName();
//                String contentType=item.getContentType();
//                long size=item.getSize();
//
//                System.out.println("fieldName = " + fieldName);
//                System.out.println("fileName = " + fileName);
//                System.out.println("contentType = " + contentType);
//                System.out.println("size = " + size);
//
//
//
//                System.out.println("uploadDir = " + uploadDir);
//
//                item.write(Paths.get(uploadDir,fileName));
//
//            }
//        }


        Map<String,List<FileItem>> formItemMap=fileUpload.parseParameterMap(request);

        //非文件数据
        String name=formItemMap.get("name").get(0).getString();

        System.out.println("name = " + name);

        //文件数据
        List<FileItem> fileItems=formItemMap.get("file");
        for (FileItem fileItem : fileItems) {

            if(fileItem.getName().endsWith(".jsp")){
                request.setAttribute("message","不能上传.jsp文件");
                request.getRequestDispatcher("/fileupload.jsp").forward(request,response);
                return;
            }



            fileItem.write(Paths.get(uploadDir,fileItem.getName()));
        }















    }
}
